Thread question



Hi,

I create two threads. Both of them will execute the same function as
below:

pthread_t thread1, thread2;

void
printids(const char *s)
{
pid_t pid;
pthread_t tid;

pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}

void *
thr_fn(void *arg)
{
printids("new thread: ");
return((void *)0);
}

int
main(void)
{
int err1, err2;

err1 = pthread_create(&thread1, NULL, thr_fn, NULL);
err2 = pthread_create(&thread2, NULL, thr_fn, NULL);

.......
}

Since the two threads execute the same function. They share the same
variables, right?
Will they conflict with each other? Should I use mutex or semaphore?
The above code is a simple example. Generally, if two threads execute
the same function, should mutex be used to prevent conflicting?

Thanks.

Jack

.



Relevant Pages

  • Re: Thread question
    ... pid_t pid; ... int err1, err2; ... Since the two threads execute the same function. ...
    (comp.os.linux.development.apps)
  • pthread error
    ... void printids{ ... pid_t pid; ... pthread_t tid; ...
    (comp.lang.c)
  • pthread error
    ... void printids{ ... pid_t pid; ... pthread_t tid; ...
    (comp.unix.programmer)
  • Re: Thread question
    ... pid_t pid; ... pthread_t tid; ... Since the two threads execute the same function. ...
    (comp.unix.programmer)
  • Re: Thread question
    ... void ff ... printids(const char *s) ... pid_t pid; ... pthread_t tid; ...
    (comp.os.linux.development.apps)