Berkeley CSUA MOTD:Entry 19972
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/07/09 [General] UID:1000 Activity:popular
7/9     

2000/12/1 [Computer/SW/Languages] UID:19972 Activity:nil
11/30   I'm having a problem running a toy pthreads program on FreeBSD. I ran
        strace on it and it looks like it exits after SIGUSR1 or SIGUSR2. The
        same program seems to run fine on (dare I say it) Linux. Any ideas?

#include <stdio.h>
#include <pthread.h>

void print_msg( void *ptr ) {
char *message;
message = (char *) ptr;
printf("%s ", message);
}

int main(int argc, char **argv) {

pthread_t thread1, thread2;
char *message1 = "Hello";
char *message2 = "World";

pthread_create(&thread1, NULL, (void*)&print_msg, (void*) message1);
pthread_create(&thread2, NULL, (void*)&print_msg, (void*) message2);

return 0;
}
        \_ Insert after the creates:

           pthread_join(thread1, NULL);
           pthread_join(thread2, NULL);

           I have no idea what I'm doing. -clueless-sodan
           \_ Yep, you definitely to somehow block so that the threads
              have time to execute.  Otherwise main() will just
              exit; whether or not it waits for threads to run to
              termination is implementation dependent.
              \_ Yeah, I just figured out that this is true. I guess
                 this is one other aspect where LinSUX tries to act
                 smart for you. The annoying thing is that I copied
                 the example right out of the pthreads book from Sun.
                 The Sun Press authors generally don't leave this sort
                 of thing out. (Yes the book is on pthreads, not solaris
                 threads).
        \_ Build with gcc -pthread?
           \_ I compiled with cc -g -Wall -pthread -o thread_test thread_test.c
              the two print_msg functions never get called.
              \_ Please follow the clueless-sodan's advice. -clueless-sodan
2025/07/09 [General] UID:1000 Activity:popular
7/9     

You may also be interested in these entries...
2014/1/14-2/5 [Computer/SW/Languages/C_Cplusplus] UID:54763 Activity:nil
1/14    Why is NULL defined to be "0" in C++ instead of "((void *) 0)" like in
        C?  I have some overloaded functtions where one takes an integer
        parameter and the other a pointer parameter.  When I call it with
        "NULL", the compiler matches it with the integer version instead of
        the pointer version which is a problem.  Other funny effect is that
        sizeof(NULL) is different from sizeof(myPtr).  Thanks.
	...
2012/7/19-11/7 [Computer/SW/Languages/C_Cplusplus] UID:54439 Activity:nil
7/19    In C or C++, how do I write the code of a function with variable
        number of parameters in order to pass the variable parameters to
        another function that also has variable number of parameters?  Thanks.
        \_ The usual way (works on gcc 3.0+, Visual Studio 2005+):
               #define foo(fmt, ...) printf(fmt, ##__VA_ARGS__)
           The cool new way (works on gcc 4.3+):
	...
2010/8/12-9/7 [Computer/SW/Languages] UID:53921 Activity:nil
8/12    Judge Walker denies Stay. Prop 8 null and void from next Wednesday:
        <DEAD>ecf.cand.uscourts.gov/cand/09cv2292<DEAD>
	...
2010/6/4-30 [Computer/SW/Languages/C_Cplusplus] UID:53849 Activity:nil
6/4     Is this valid C++ code?
        std::string getStr(void) {
            std::string str("foo");
            return str;
        }
        void foo(char *s);
	...
2006/2/21-23 [Computer/SW/Languages/Java] UID:41939 Activity:nil
2/21    What is the best way to get the current method name in Java?  So far
        I have seen 3 approaches:
        1. Create new Exception, grab the first frame off its stack trace.
           Inelegant, requires creating a stack trace (expensive).
           \_ Yeah it sucks.  Yeah it does much more work than you need.
              Yeah it is really fragile and may break when you switch java
	...
2006/2/1-3 [Consumer/CellPhone, Computer/SW/Security] UID:41652 Activity:low
2/1     Dear old farts. What was the consumer end of telecomm like before
        the 1983 divestiture of AT&T into 7 baby Bells, in terms of price
        for consumers, sound quality, reliability, and service?
        \_ Most of you youngin' were too young to remember this but back
           then long distance calls were prohibitively expensive. On the
           other hand, you didn't have tons of long distance carriers to
	...
2006/1/23-25 [Computer/SW/Languages/Java] UID:41480 Activity:nil
1/23    I'm trying to make somebody else's code thread-safe and it seems like
        my synchronization blocks are not being respected.  Does anyone know
        a problem with this code?
        class FooPoller {
          protected static Boolean lock = new Boolean(true);
          private static void poll() {
	...