PThread liberary doubt


 
Thread Tools Search this Thread
Top Forums Programming PThread liberary doubt
# 1  
Old 01-27-2009
PThread liberary doubt

I would like to ask that PrintHello accepts a void pointer but how come you can equate it to an integer directly ?
" tid = (int)threadid; "
while at the same time instead if a pointer a normal variable was passed into this function
" pthread_create(&threads[t], NULL, PrintHello, (void *)t); "

the above construct is taken from
https://computing.llnl.gov/tutorials...ssingArguments

although this is necessary because if we &t than this variable is not thread safe it might be incremented before the thread would start execution,
but passing integer variable where pointer is expected seem unconventional So, is there a proper method to do this.

<code>
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
int tid;
tid = (int)threadid;
printf("Hello World! It's me, thread #%d!\n", tid);
pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc, t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}

</code>
# 2  
Old 02-04-2009
Quote:
Originally Posted by digi123
I would like to ask that PrintHello accepts a void pointer but how come you can equate it to an integer directly ?
This has to do with C programming, not UNIX programming, AFAIK. Are you asking why can a void pointer also be an integer? Because on almost all hardware platforms today, an int is the same size as a pointer. (Sometimes on 64-bit platforms, an int will be 32 bits while the pointer will be 64-bits -- for backwards compatibility.)

Quote:
safe it might be incremented before the thread would start execution,
but passing integer variable where pointer is expected seem unconventional
Maybe it's unconventional to a Java programmer, but to C programmers, that's quite common.
# 3  
Old 02-07-2009
Read up about the concept of programming models.

Long long time ago when I had a full head of hair and 16-bit processors dominated the computing world, ILP16 was the standard C programming model i.e. an int = long = pointer. Then with the arrival of 32-bit processors the UNIX world moved to IPL32 where again an int = long = pointer.

Later with the arrival of 64-bit processors, the Aspen WG (essentially all the UNIX vendors) agreed to the LP64 programming model where int remained 32-bits and long = pointer = 64 bits. This enabled the 2Gb filesystem limitation to be natively overcome without using the LFS (Large File Summit) extensions.

Note - 64 bit Windows uses the IPL64 programming model.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

pthread()

I have a while loop like so: while (counter (file1)); how can I pass that into a pthread_create()? I was thinking ... while(pthread_create(&path, NULL, counter, file)); is that right? (1 Reply)
Discussion started by: l flipboi l
1 Replies

2. Programming

Help with pthread error

I have written a C code and when i compile it there are 0 warnings and 0 errors, but when i try to run apears: ./client: symbol lookup error: ./client: undefined symbol: pthread_create, version GLIBC_2.1 the part of the code where i have the pthread_creat is: int serverConection(int... (5 Replies)
Discussion started by: SuperStout
5 Replies

3. Ubuntu

pthread problem

Hi all, I wrote some code in c, using pthread (I configured the linker and compiler in eclipse IDE first). #include <pthread.h> #include "starter.h" #include "UI.h" Page* MM; Page* Disk; PCB* all_pcb_array; void* display_prompt(void *id){ printf("Hello111\n"); return... (1 Reply)
Discussion started by: elad2109
1 Replies

4. UNIX for Advanced & Expert Users

pthread

I am so confused about the user threads and kernel threads.Suppose I created a thread using pthread create call in Linux ,whether it will be a user thread or kernel thread.If it user thread,then how its map to kernel thread. I heard about the M:1,M:N,1:1 mapping methods.Which method linux is... (1 Reply)
Discussion started by: sujith4u87
1 Replies

5. Programming

How can I parallize using pthread

Hi all, How can i parallize this code in pthread? for(round=1;round<=16;round++) { Expansion(mid, 17 - round - 1, left); Expansion(mid, round - 1, right); round++; Expansion(right, 17 - round - 1, mid); Expansion(left, round - 1,mid); } Whereby each loop depend on the... (2 Replies)
Discussion started by: m_enayah
2 Replies

6. Programming

PThread and More Than 1 Core

Hello all. I have made an application in C using pthreads. My problem is that my program seems to be only using 1 of my cores when I create multiple threads. I have 4 cores (Q9300). I am using Ubuntu 8.04. I do believe the problem is that on linkage it's using a pthread emulation package... (4 Replies)
Discussion started by: LightRaven
4 Replies

7. UNIX for Dummies Questions & Answers

Where are the pthread functions??

When I use some of the pthread functions: pthread_join, pthread_exit they work perfect. But when I look in the pthread.h file I can't seem to find any implementations of the functions...where are they hiding?? (2 Replies)
Discussion started by: bigblop
2 Replies

8. Programming

pthread.h

hallo 2 al can anyone pls tell me where and how can i find and install the pthread.h lib ? thx :cool: (2 Replies)
Discussion started by: XinU*
2 Replies

9. Programming

pthread

consider if the thread routine returns any void pointer while calling pthread_join, the thread resources are freed and the thread will be terminated when the main thread is exit ,that is my assumption whether it is true how do we find whether the thread is alive or terminated how do we find... (0 Replies)
Discussion started by: MKSRaja
0 Replies

10. Programming

More about Pthread

Can someone point to a link where I can get good info about pthread? thanx.. :) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question