| 1 |
#include <Inventor/@Gui@/So@[email protected]> |
| 2 |
#include <Inventor/@Gui@/viewers/So@[email protected]> |
| 3 |
#include <Inventor/nodes/SoSeparator.h> |
| 4 |
#include <Inventor/actions/SoWriteAction.h> |
| 5 |
#include <Inventor/SoInput.h> |
| 6 |
#include <Inventor/SoOutput.h> |
| 7 |
#include <Inventor/SoDB.h> |
| 8 |
|
| 9 |
#include <Inventor/C/threads/thread.h> |
| 10 |
#include <Inventor/threads/SbThread.h> |
| 11 |
|
| 12 |
// This application attempts to check if applying an SoWrite action to |
| 13 |
// a scenegraph is thread-safe. A scenegraph is read from the file |
| 14 |
// specified on the command line. Each thread applies an SoWriteAction |
| 15 |
// on the scene. The idea is that if the input file is big enough, |
| 16 |
// each thread will not have time to finish its write operation before |
| 17 |
// the next thread starts. |
| 18 |
|
| 19 |
// Each thread will output an iv file. The contents of this file |
| 20 |
// should of course be the same for all the threads. Verify this by |
| 21 |
// doing something like: |
| 22 |
|
| 23 |
// $ for i in `seq 2 10`; do |
| 24 |
// echo 'diff 1.iv' $i.iv |
| 25 |
// diff 1.iv $i.iv; |
| 26 |
// done |
| 27 |
|
| 28 |
|
| 29 |
class thread_data { |
| 30 |
public: |
| 31 |
SoSeparator * root; |
| 32 |
SbString filename; |
| 33 |
int thread_number; |
| 34 |
}; |
| 35 |
|
| 36 |
static void * thread_callback(void * closure) |
| 37 |
{ |
| 38 |
thread_data * data = (thread_data *) closure; |
| 39 |
|
| 40 |
SoOutput out; |
| 41 |
SbBool ok = out.openFile(data->filename.getString()); |
| 42 |
if (!ok) { |
| 43 |
fprintf(stderr, "unable to open output file %s\n", data->filename.getString()); |
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
fprintf(stdout, "start write, thread nr. %i\n", data->thread_number); |
| 48 |
SoWriteAction wa(&out); |
| 49 |
wa.apply(data->root); |
| 50 |
fprintf(stdout, "finished write, thread nr. %i\n", data->thread_number); |
| 51 |
|
| 52 |
return NULL; |
| 53 |
} |
| 54 |
|
| 55 |
int main(int argc, char ** argv) |
| 56 |
{ |
| 57 |
if (argc < 2) { |
| 58 |
printf("specify an input file\n"); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
@WIDGET@ mainwin = So@Gui@::init(argv[0]); |
| 63 |
|
| 64 |
// read scene |
| 65 |
SoInput in; |
| 66 |
SbBool ok = in.openFile(argv[1]); |
| 67 |
if (!ok) { |
| 68 |
fprintf(stderr, "unable to open file %s\n", argv[1]); |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
SoSeparator * root = SoDB::readAll(&in); |
| 73 |
if (!root) { |
| 74 |
fprintf(stderr, "unable to read file %s\n", argv[1]); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
root->ref(); |
| 78 |
|
| 79 |
// create 10 threads that applies an SoWriteAction to the scene |
| 80 |
for (int i = 1; i <= 10; i++) { |
| 81 |
thread_data * data = new thread_data; |
| 82 |
data->root = root; |
| 83 |
data->thread_number = i; |
| 84 |
data->filename.sprintf("%i.iv", i); |
| 85 |
(void)SbThread::create(thread_callback, data); |
| 86 |
} |
| 87 |
|
| 88 |
// throw up a viewer and show the scene while we wait for the |
| 89 |
// trheads to finish |
| 90 |
// Use one of the convenient SoQt viewer classes. |
| 91 |
So@Gui@ExaminerViewer * eviewer = new So@Gui@ExaminerViewer(mainwin); |
| 92 |
eviewer->setSceneGraph(root); |
| 93 |
eviewer->show(); |
| 94 |
// Pop up the main window. |
| 95 |
So@Gui@::show(mainwin); |
| 96 |
|
| 97 |
// Loop until exit. |
| 98 |
So@Gui@::mainLoop(); |
| 99 |
|
| 100 |
// Clean up resources. |
| 101 |
delete eviewer; |
| 102 |
root->unref(); |
| 103 |
|
| 104 |
|
| 105 |
return 0; |
| 106 |
} |