00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "libpthreadexample.h"
00033 #include <e32base.h>
00034 #include <e32std.h>
00035 #include <e32cons.h>
00036 #include <pthread.h>
00037 #include <f32file.h>
00038 #include "unistd.h"
00039
00040
00041 _LIT(KTestFile,"c:\\newfile.txt");
00042
00043
00044 LOCAL_D CConsoleBase* console;
00045 pthread_mutex_t mutex;
00046
00047
00048
00049
00050
00051 void *TestFunction1L(void*)
00052 {
00053
00054 RFs aFs;
00055 User::LeaveIfError(aFs.Connect());
00056
00057 RFile file;
00058 pthread_mutex_lock(&mutex);
00059 TInt pos = 0;
00060 for(int i=0; i<5; i++)
00061 {
00062
00063 CleanupClosePushL(file);
00064 file.Open(aFs,KTestFile,EFileRead|EFileWrite);
00065
00066 _LIT8(KWriteBuf,"One, \n");
00067 file.Seek(ESeekEnd, pos );
00068 file.Write(pos ,KWriteBuf);
00069 CleanupStack::PopAndDestroy(&file);
00070 file.Close();
00071 User::After(1000000);
00072 }
00073 aFs.Close();
00074 pthread_mutex_unlock(&mutex);
00075 return NULL;
00076 }
00077
00078
00079
00080
00081
00082 void *TestFunction2L(void*)
00083 {
00084
00085 RFs aFs;
00086 User::LeaveIfError(aFs.Connect());
00087
00088 RFile file;
00089 pthread_mutex_lock(&mutex);
00090 TInt pos = 0;
00091 for(int i=0; i<5; i++)
00092 {
00093
00094 CleanupClosePushL(file);
00095 file.Open(aFs, KTestFile, EFileRead|EFileWrite);
00096
00097 _LIT8(KWriteBuf, "Two, \n");
00098 file.Seek(ESeekEnd, pos );
00099 file.Write(pos, KWriteBuf);
00100 CleanupStack::PopAndDestroy(&file);
00101 file.Close();
00102 User::After(1000000);
00103 }
00104 aFs.Close();
00105 pthread_mutex_unlock(&mutex);
00106 return NULL;
00107 }
00108
00109
00110
00111
00112
00113 void *TestFunction3L(void*)
00114 {
00115
00116 RFs aFs;
00117 User::LeaveIfError(aFs.Connect());
00118
00119 RFile file;
00120 pthread_mutex_lock(&mutex);
00121 TInt pos = 0;
00122 for(int i=0; i<5;i++ )
00123 {
00124
00125 CleanupClosePushL(file);
00126 file.Open(aFs,KTestFile,EFileRead|EFileWrite);
00127
00128 _LIT8(KWriteBuf,"Three ,\n");
00129 file.Seek(ESeekEnd , pos );
00130 file.Write(pos ,KWriteBuf);
00131 CleanupStack::PopAndDestroy(&file);
00132 file.Close();
00133 User::After(1000000);
00134 }
00135 aFs.Close();
00136 pthread_mutex_unlock(&mutex);
00137 return NULL;
00138 }
00139
00140
00141
00142
00143
00144
00145 int CreateThreadL()
00146 {
00147
00148 pthread_t threadOne;
00149 pthread_t threadTwo;
00150 pthread_t threadThree;
00151
00152 _LIT(KMain ,"in main\n" );
00153 console->Write(KMain);
00154
00155 RFs aFs;
00156 User::LeaveIfError(aFs.Connect());
00157
00158
00159 RFile file;
00160 CleanupClosePushL(file);
00161 file.Create(aFs ,KTestFile,EFileRead|EFileWrite);
00162 CleanupStack::PopAndDestroy(&file);
00163 file.Close();
00164 aFs.Close();
00165
00166 if((pthread_mutex_init(&mutex,NULL)) != 0)
00167 {
00168 return -1;
00169 }
00170
00171
00172 TInt err1 = pthread_create(&threadOne,NULL,TestFunction1L, NULL);
00173 User::LeaveIfError(err1);
00174 _LIT(KThreadOneCreated , "ThreadOne created\n");
00175 console->Write(KThreadOneCreated);
00176
00177
00178 TInt err2 = pthread_create(&threadTwo,NULL,TestFunction2L, NULL);
00179 User::LeaveIfError(err2);
00180 _LIT(KThreadTwoCreated , "ThreadTwo created\n");
00181 console->Write(KThreadTwoCreated);
00182
00183
00184 TInt err3 = pthread_create(&threadThree,NULL,TestFunction3L, NULL);
00185 User::LeaveIfError(err3);
00186 _LIT(KThreadThreeCreated , "ThreadThree created\n");
00187 console->Write(KThreadThreeCreated);
00188
00189
00190
00191 _LIT(KWait , "Waiting for child threads to complete execution\n");
00192 console->Write(KWait);
00193
00194 _LIT(KJoinError , "Error in pthread_join()");
00195
00196 if(pthread_join(threadOne, NULL) != 0)
00197 {
00198 console->Write(KJoinError);
00199 }
00200 else
00201 {
00202 _LIT(KRetThreadOne ,"Returned from threadone\n");
00203 console->Write(KRetThreadOne);
00204 }
00205
00206
00207 if(pthread_join(threadTwo, NULL) != 0)
00208 {
00209 console->Write(KJoinError);
00210 }
00211 else
00212 {
00213 _LIT(KRetThreadTwo ,"Returned from threadtwo\n");
00214 console->Write(KRetThreadTwo);
00215 }
00216
00217
00218 if(pthread_join(threadThree, NULL) != 0)
00219 {
00220 console->Write(KJoinError);
00221 }
00222 else
00223 {
00224 _LIT(KRetThreadThree ,"Returned from threadthree\n");
00225 console->Write(KRetThreadThree);
00226 }
00227
00228 pthread_mutex_destroy(&mutex);
00229 return -1;
00230 }
00231
00232
00233
00234 LOCAL_C void MainL()
00235 {
00236 _LIT(KHello , "Welcome to the LibpThread example, we will create three threads and use them to print data to \\epoc32\\winscw\\c\\newfile.txt\n\n");
00237 console->Write(KHello);
00238 CreateThreadL();
00239 }
00240
00241 LOCAL_C void DoStartL()
00242 {
00243 MainL();
00244 }
00245
00246
00247
00248 GLDEF_C TInt E32Main()
00249 {
00250
00251 __UHEAP_MARK;
00252 CTrapCleanup* cleanup = CTrapCleanup::New();
00253
00254 _LIT(KTextConsoleTitle, "Console");
00255 TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00256 if (createError)
00257 return createError;
00258
00259 TRAPD(mainError, DoStartL());
00260 _LIT(KTextFailed, " failed, leave code = %d");
00261 if (mainError)
00262 console->Printf(KTextFailed, mainError);
00263 _LIT(KTextPressAnyKey, " [press any key]\n");
00264 console->Printf(KTextPressAnyKey);
00265 console->Getch();
00266 delete console;
00267 delete cleanup;
00268 __UHEAP_MARKEND;
00269 return KErrNone;
00270 }
00271