![Page 1: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/1.jpg)
Concurrency – Multithreading
ZhaoguoWang
![Page 2: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/2.jpg)
Example longbigloop(int*arr){longr=0;for(inti=0;i<8;i++)r+=arr[i];returnr;}intmain(){int*arr=malloc(8*sizeof(int));...longr=bigloop(arr,l);...}
How to improve the performance with multicore?
![Page 3: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/3.jpg)
Parallelization
CPU0 CPU1 CPU2 CPU3
0 1 2 3 4 5 6 7
bigloop: 0à7
![Page 4: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/4.jpg)
Parallelization
CPU0 CPU1 CPU2 CPU3
0 1
bigloop: 0à1
2 3
bigloop: 2à3
4 5
bigloop: 4à5
6 7
bigloop: 6à7
Performance can be improved by 4X
![Page 5: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/5.jpg)
Parallelization
CPU0 CPU1 CPU2 CPU3
0 1
bigloop: 0à1
2 3
bigloop: 2à3
4 5
bigloop: 4à5
6 7
bigloop: 6à7
What's concurrency? - things happening "simultaneously”
• multiple CPU cores concurrently executing instructions • CPU and I/O devices concurrently doing processing
Performance can be improved by 4X
![Page 6: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/6.jpg)
Concurrency
What's concurrency? – things happening "simultaneously”
• multiple CPU cores concurrently executing instructions • CPU and I/O devices concurrently doing processing
Why write concurrent programs? – speed up programs using multiple CPUs – speed up programs by interleaving CPU processing
and I/O.
![Page 7: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/7.jpg)
In this lecture
What's concurrency? – things happening "simultaneously”
• multiple CPU cores concurrently executing instructions • CPU and I/O devices concurrently doing processing
Why write concurrent programs? – speed up programs using multiple CPUs – speed up programs by interleaving CPU processing
and I/O.
![Page 8: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/8.jpg)
How to write concurrent programs?
Use multiple processes – Each process uses a different CPU – Different processes runs different tasks
• They have separated address space • It is difficult to communicate with each other
Use multiple threads
![Page 9: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/9.jpg)
In this lecture
Use multiple processes – Each process uses a different CPU – Different processes runs different tasks
• They have separated address space • It is difficult to communicate with each other
Use multiple threads
![Page 10: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/10.jpg)
Multiple threads (Multithreading)
CPU0 CPU1 CPU2 CPU3
0 1 2 3 4 5 6 7
bigloop: 0à7
Process longbigloop(int*arr){longr=0;for(inti=0;i<8;i++)r+=arr[i];returnr;}intmain(){int*arr=malloc(8*sizeof(int));...longr=bigloop(arr,l);...}
![Page 11: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/11.jpg)
Multiple threads (Multithreading)
CPU0 CPU1 CPU2 CPU3
0 1
bigloop: 0à1
2 3
bigloop: 2à3
4 5
bigloop: 4à5
6 7
bigloop: 6à7
thread 0 thread 1 thread 2 thread 3
Process
![Page 12: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/12.jpg)
Multiple threads (Multithreading)
CPU0 CPU1 CPU2 CPU3
0 1
bigloop: 0à1
2 3
bigloop: 2à3
4 5
bigloop: 4à5
6 7
bigloop: 6à7
thread 0 thread 1 thread 2 thread 3
Process
Single process, multiple threads – Share the same memory space – Has its own stack – Has its own control flow
![Page 13: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/13.jpg)
Share the memory space Mainmemory
...
VirtualAddressspace
vaddr1 vaddr2 vaddr3 …
Level 0
Level 1 Level 2 Level 3
Page Table 1
vaddr1 vaddr2 vaddr3 …
Level 0
Level 1 Level 2 Level 3
Page Table 2
Process 1
Process 2
Different processes have different page tables
![Page 14: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/14.jpg)
Share the memory space Mainmemory
...
VirtualAddressspace
vaddr1 vaddr2 vaddr3 …
Level 0
Level 1 Level 2 Level 3
Page Table 1
vaddr1 vaddr2 vaddr3 …
Level 0
Level 1 Level 2 Level 3
Page Table 2
Process 1
Process 2
Different processes have different page tables
Mainmemory
...
VirtualAddressspace
vaddr1 vaddr2 vaddr3 …
Level 0
Level 1 Level 2 Level 3
Page Table 1
vaddr1 vaddr2 vaddr3 …
Thread 1
Thread 2
Different threads of the same process share the same page table
![Page 15: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/15.jpg)
Thread local stack Kernel virtual memory
Shared libraries
Runtime heap
User stack
Unused 0
0x400000
Read/write segment
Read-only segment
%rsp (stack pointer)
Memory invisible to user code
brk
Loaded from the executable file
thread 0 thread 1 thread 2 thread 3
Process 1
![Page 16: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/16.jpg)
Thread local stack Kernel virtual memory
Shared libraries
Runtime heap
User stack 0
Unused 0
0x400000
Read/write segment
Read-only segment
sp0
Memory invisible to user code
brk
Loaded from the executable file
thread 0 thread 1 thread 2 thread 3
Process 1
User stack 1 sp1
User stack 2
User stack 3 sp2
sp3
Each thread has its own stack segment - Each thread has its own stack pointer - Store the stack pointer into the %rsp
before running
CPU 0 sp0 RSP:
CPU 1 sp1 RSP:
CPU 2 sp2 RSP:
CPU 3 sp3 RSP:
![Page 17: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/17.jpg)
Own control flow Kernel virtual memory
Shared libraries
Runtime heap
User stack 0
Unused 0
0x400000
Read/write segment
Read-only segment
sp0
Memory invisible to user code
brk
Loaded from the executable file
thread 0 thread 1 thread 2 thread 3
Process 1
User stack 1 sp1
User stack 2
User stack 3 sp2
sp3
Each thread loads PC register of local CPU with different instructions
CPU 0 addr1 PC:
movq … IR:
sp0 RSP:
CPU 1 addr2 PC:
addq … IR:
sp1 RSP:
CPU 2 addr3 PC:
mulq … IR:
sp2 RSP:
CPU 3 addr4 PC:
subq … IR:
sp3 RSP:
![Page 18: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/18.jpg)
POSIX thread interface
POSIX: Portable Operating System Interface – POSIX defines the application programming interface
(API) for software compatibility with variants of Unix and other operating systems
Thread interface defined by POSIX – pthread_create: create a new thread – pthread_join: wait for the target thread terminated
![Page 19: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/19.jpg)
pthread_create #include<pthread.h>intpthread_create(pthread_t*thread_id,constpthread_attr_t*attr,void*(*start_routine)(void*),void*arg);Create a new thread
– It executes start_routine with arg as its sole argument. – Its attribute is specified by attr– Upon successful completion, it will store the ID of the created thread
in the location referenced by thread_id.
Return value – zero: success – non-zero (error number): fail
![Page 20: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/20.jpg)
Example 1 – Create
gcc create.c -lpthread
void*func(void*arg){printf("Thisisthecreatedthread\n");returnNULL;}intmain(intargc,char*argv[]){pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);if(r!=0){printf(“createthreadfailed”);return1;}return0;}
![Page 21: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/21.jpg)
Example 1 – Create
Main thread returns before the created thread finishes. - Automatically terminate and reclaim the
created thread.
void*func(void*arg){printf("Thisisthecreatedthread\n");returnNULL;}intmain(intargc,char*argv[]){pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);if(r!=0){printf(“createthreadfailed”);return1;}return0;}
gcc create.c -lpthread
![Page 22: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/22.jpg)
pthread_join #include<pthread.h>intpthread_join(pthread_tthread,void**ret_ptr);
Wait for the target thread to finish – The target thread is specified by thread– Upon success, the return value of the created thread will be
available in the location referenced by ret_ptr.
Return value – zero: success – non-zero (error number): fail
![Page 23: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/23.jpg)
Example 2 – Join void*func(void*arg){printf("Thisisthecreatedthread\n");returnNULL;}intmain(intargc,char*argv[]){pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);if(r!=0)...r=pthread_join(tid,NULL);if(r!=0)...return0;}
![Page 24: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/24.jpg)
Example 3 – Parameter void*func(void*arg){intp=*(int*)arg;p=p+1;return&p;}intmain(intargc,char*argv[]){intparam=100;pthread_ttid;intr=pthread_create(&tid,NULL,&func,(void*)¶m);...int*res=NULL;r=pthread_join(tid,&res);...printf("result:addr%lxval%d\n",res,*res);return0;}
Question – what is expected result ?
![Page 25: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/25.jpg)
Example 3 – Parameter void*func(void*arg){intp=*(int*)arg;p=p+1;return&p;}intmain(intargc,char*argv[]){intparam=100;pthread_ttid;intr=pthread_create(&tid,NULL,&func,(void*)¶m);...int*res=NULL;r=pthread_join(tid,&res);...printf("result:addr%lxval%d\n",res,*res);return0;}
p is on the stack of the created thread -- it is destroyed when the thread terminates
![Page 26: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/26.jpg)
Example 3 – Parameter void*func(void*arg){intp=*(int*)arg;p=p+1;int*r=(void*)malloc(sizeof(int));*r=p;return(void*)r;}intmain(intargc,char*argv[]){intparam=100;pthread_ttid;intr=pthread_create(&tid,NULL,&func,(void*)¶m);...int*res=NULL;r=pthread_join(tid,&res);...printf("result:addr%lxval%d\n",res,*res);return0;}
![Page 27: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/27.jpg)
void*func(void*arg){intp=*(int*)arg;p=p+1;int*r=(void*)malloc(sizeof(int));*r=p;return(void*)r;}intmain(intargc,char*argv[]){intparam=100;pthread_ttid;intr=pthread_create(&tid,NULL,&func,(void*)¶m);...int*res=NULL;r=pthread_join(tid,&res);...printf("result:addr%lxval%d\n",res,*res);free(res)return0;}
Example 3 – Parameter
![Page 28: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/28.jpg)
Example 4 – Interleave void*func(void*arg){printf(“1”);}intmain(intargc,char*argv[]){printf(“0”);pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);...printf(“2”);...return0;}
Question – what is the expected result ?
![Page 29: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/29.jpg)
Example 4 – Interleave void*func(void*arg){printf(“1”);}intmain(intargc,char*argv[]){printf(“0”);pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);...printf(“2”);...return0;}
Question – what is the expected result ?
Answer: 012 or 021
![Page 30: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/30.jpg)
Example 4 – Interleave void*func(void*arg){printf(“1”);}intmain(intargc,char*argv[]){printf(“0”);pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);...printf(“2”);...return0;}
Question – what is the expected result ?
Answer: 012 or 021
012 main thread
printf(“0”)
pthread_create
printf(“2”)
main thread
printf(“1”)
time
![Page 31: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/31.jpg)
Example 4 – Interleave void*func(void*arg){printf(“1”);}intmain(intargc,char*argv[]){printf(“0”);pthread_ttid;intr=pthread_create(&tid,NULL,&func,NULL);...printf(“2”);...return0;}
Question – what is the expected result ?
Answer: 012 or 021
021 main thread
printf(“0”)
pthread_create
printf(“2”)
main thread
printf(“1”)
time
![Page 32: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/32.jpg)
Example 5 – Stack, Heap, Global intglobal=0;void*write(void*arg){intlocal=0;local=100;global=100;int*ptr=(int*)arg;(*ptr)=100;}intmain(intargc,char*argv[]){int*p=(int*)malloc(sizeof(int));pthread_ttid;intr=pthread_create(&tid,NULL,&write,(void*)p);...sleep(1)intr=pthread_create(&tid,NULL,&read,(void*)p);...return0;}
void*read(void*arg){intlocal;printf("local%dglobal%dheap%d\n",local,global,*(int*)arg);returnNULL;}
![Page 33: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/33.jpg)
Example 5 – Stack, Heap, Global
Kernel virtual memory
Shared libraries
Runtime heap
User stack 0
Unused 0
0x400000
Read/write segment
Read-only segment
sp0
Memory invisible to user code
brk
Loaded from the executable file
write read
Process 1
User stack 1 sp1 local
local
*p
global
![Page 34: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/34.jpg)
Example 5 – Stack, Heap, Global intglobal=0;void*write(void*arg){intlocal=0;local=100;global=100;int*ptr=(int*)arg;(*ptr)=100;}intmain(intargc,char*argv[]){int*p=(int*)malloc(sizeof(int));pthread_ttid;intr=pthread_create(&tid,NULL,&write,(void*)p);...sleep(1)intr=pthread_create(&tid,NULL,&read,(void*)p);...return0;}
void*read(void*arg){intlocal;printf("local%dglobal%dheap%d\n",local,global,*(int*)arg);returnNULL;}
How can read access local in write?
![Page 35: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/35.jpg)
Example 5 – Stack, Heap, Global intglobal=0;int*local_addr=0;void*write(void*arg){intlocal=0;local_addr=&local;local=100;global=100;int*ptr=(int*)arg;(*ptr)=100;sleep(10);}intmain(intargc,char*argv[]){int*p=(int*)malloc(sizeof(int));pthread_ttid;intr=pthread_create(&tid,NULL,&write,(void*)p);...sleep(1)intr=pthread_create(&tid,NULL,&read,(void*)p);...return0;}
void*read(void*arg){printf("local%dglobal%dheap%d\n",*local_addr,global,*(int*)arg);returnNULL;}
![Page 36: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/36.jpg)
void*func(void*arg){intp=*(int*)arg;p=p+1;int*r=(void*)malloc(sizeof(int));*r=p;return(void*)r;}intmain(intargc,char*argv[]){intparam=100;pthread_ttid;intr=pthread_create(&tid,NULL,&func,(void*)¶m);...int*res=NULL;r=pthread_join(tid,&res);...printf("result:addr%lxval%d\n",res,*res);free(res)return0;}
Example 3 – Review Question – can we get rid of r in func?
![Page 37: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/37.jpg)
void*func(void*arg){int*p=(int*)arg;*p=*p+1;returnNULL;}intmain(intargc,char*argv[]){intparam=100;pthread_ttid;intr=pthread_create(&tid,NULL,&func,(void*)¶m);...int*res=NULL;r=pthread_join(tid,&res);...printf("result:%d\n",param);return0;}
Example 3 – Review Question – can we get rid of r in func?
![Page 38: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/38.jpg)
Example 6 – bigloop #defineLEN1000000000longbigloop(int*arr){longr=0;for(inti=0;i<LEN;i++)r+=arr[i];returnr;}intmain(){int*arr=malloc(LEN*sizeof(int));...longr=bigloop(arr);...}
Parallelize bigloop into two threads
![Page 39: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/39.jpg)
Example 6 – bigloop #defineLEN1000000000void*loop_thr1(void*arg){long*r=malloc(sizeof(long));int*arr=(int*)arg;for(inti=0;i<LEN/2;i++)(*r)+=arr[i];return(void*)r;}intmain(){int*arr=malloc(LEN*sizeof(int));pthread_ttid1,tid2;intr=pthread_create(&tid,NULL,&loop_thr1,(void*)arr);...r=pthread_create(&tid,NULL,&loop_thr2,(void*)arr);...}
void*loop_thr2(void*arg){long*r=malloc(sizeof(long));int*arr=(int*)arg;for(inti=LEN/2;i<LEN;i++)(*r)+=arr[i];return(void*)r;}
![Page 40: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/40.jpg)
Example 6 – bigloop #defineLEN1000000000void*loop_thr1(void*arg){long*r=malloc(sizeof(long));int*arr=(int*)arg;for(inti=0;i<LEN/2;i++)(*r)+=arr[i];return(void*)r;}intmain(){int*arr=malloc(LEN*sizeof(int));pthread_ttid1,tid2;intr=pthread_create(&tid,NULL,&loop_thr1,(void*)arr);...r=pthread_create(&tid,NULL,&loop_thr2,(void*)arr);...}
void*loop_thr2(void*arg){long*r=malloc(sizeof(long));int*arr=(int*)arg;for(inti=LEN/2;i<LEN;i++)(*r)+=arr[i];return(void*)r;}
Can we merge loop_thr1 with loop_thr2?
![Page 41: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/41.jpg)
Example 6 – bigloop #defineLEN1000000000typedefstruct{int*arr;intstart;intend;}loop_info;intmain(){int*arr=malloc(LEN*sizeof(int));pthread_ttid1,tid2;intr=pthread_create(&tid,NULL,&loop,(void*)p);...r=pthread_create(&tid,NULL,&loop,(void*)p);...}
void*loop(void*arg){long*r=malloc(sizeof(long));loop_info*info=(loop_info*)arg;for(inti=info->start;i<info->end;i++)(*r)+=info->arr[i];return(void*)r;}
![Page 42: Concurrency – Multithreadingnews.cs.nyu.edu/~zhaoguo/fa17-cso/notes/19-Multithreading.pdf · Read/write segment Read-only segment %rsp (stack pointer) Memory invisible to user code](https://reader034.vdocuments.mx/reader034/viewer/2022042205/5ea6cbda634e9909df326cc8/html5/thumbnails/42.jpg)
Example 6 – bigloop #defineLEN1000000000typedefstruct{int*arr;intstart;intend;}loop_info;intmain(){int*arr=malloc(LEN*sizeof(int));pthread_ttid1,tid2;intr=pthread_create(&tid,NULL,&loop,(void*)p);...r=pthread_create(&tid,NULL,&loop,(void*)p);...}
void*loop(void*arg){long*r=malloc(sizeof(long));loop_info*info=(loop_info*)arg;for(inti=info->start;i<info->end;i++)(*r)+=info->arr[i];return(void*)r;}
How to create N threads? N is passed by user and LEN is multiple times of N.