Skip to content

Commit

Permalink
add mutex lock
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 12, 2016
1 parent 3535fe8 commit 2d290e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ v=debug
include co.mk

########## options ##########
#CFLAGS += -g -fno-strict-aliasing -O2 -Wall -export-dynamic \
# -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64
CFLAGS += -g -fno-strict-aliasing -O2 -Wall -export-dynamic \
-Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64

LINKS += -g -L./lib -lcolib -lpthread -ldl
LINKS += -g -L./lib -lcolib -lpthread -ldl

COLIB_OBJS=co_epoll.o co_routine.o co_hook_sys_call.o coctx_swap.o coctx.o
#co_swapcontext.o

PROGS = colib example_poll example_echosvr example_echocli example_thread example_cond example_specific example_copystack
PROGS = colib example_poll example_echosvr example_echocli example_thread example_cond example_specific example_copystack example_closure

all:$(PROGS)

Expand Down Expand Up @@ -61,6 +61,8 @@ example_copystack:example_copystack.o
$(BUILDEXE)
example_setenv:example_setenv.o
$(BUILDEXE)
example_closure:example_closure.o
$(BUILDEXE)

dist: clean libco-$(version).src.tar.gz

Expand Down
6 changes: 5 additions & 1 deletion example_closure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ int main( int argc,char *argv[] )
{
vector< stCoClosure_t* > v;

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

int total = 100;
vector<int> v2;
co_ref( ref,total,v2 );
co_ref( ref,total,v2,m);
for(int i=0;i<10;i++)
{
co_func( f,ref,i )
{
printf("ref.total %d i %d\n",ref.total,i );
//lock
pthread_mutex_lock(&ref.m);
ref.v2.push_back( i );
pthread_mutex_unlock(&ref.m);
//unlock
}
co_func_end;
Expand Down

0 comments on commit 2d290e3

Please sign in to comment.